package com.java1412.device.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.java1412.device.domain.DeviceDTO;
import com.java1412.devicetype.controller.ImsDeviceTypeController;
import com.java1412.devicetype.domain.ImsDeviceType;
import com.java1412.devicetype.service.IImsDeviceTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.java1412.device.domain.ImsDevice;
import com.java1412.device.service.IImsDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备追加属性Controller
 *
 * @author ruoyi
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/device/device")
public class ImsDeviceController extends BaseController
{
    @Autowired
    private IImsDeviceService imsDeviceService;

    @Autowired
    private IImsDeviceTypeService imsDeviceTypeService;


    /**
     * 查询设备追加属性列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImsDevice imsDevice)
    {
        startPage();
        List<ImsDevice> list = imsDeviceService.selectImsDeviceList(imsDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备追加属性列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:export')")
    @Log(title = "设备追加属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImsDevice imsDevice)
    {
        List<ImsDevice> list = imsDeviceService.selectImsDeviceList(imsDevice);
        ExcelUtil<ImsDevice> util = new ExcelUtil<ImsDevice>(ImsDevice.class);
        util.exportExcel(response, list, "设备追加属性数据");
    }

    /**
     * 获取设备追加属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        //查询设备表中的数据
        ImsDevice imsDevice = imsDeviceService.selectImsDeviceById(id);
        //查询设备类型表中的数据
        ImsDeviceType imsDeviceType = imsDeviceTypeService.selectImsDeviceTypeById(imsDevice.getDeviceTypeId());
        DeviceDTO deviceDTO = new DeviceDTO();
        deviceDTO.setDeviceTypeId(imsDevice.getDeviceTypeId());
        deviceDTO.setDeviceName(imsDeviceType.getDeviceName());
        deviceDTO.setAppendFields(imsDeviceType.getAppendFields());
        deviceDTO.setAppendTheAttributeAndValue(imsDevice.getAppendTheAttributeAndValue());
        deviceDTO.setDeviceId(imsDevice.getId());
        return success(deviceDTO);
    }

    /**
     * 新增设备追加属性
     */
    @PreAuthorize("@ss.hasPermi('device:device:add')")
    @Log(title = "设备追加属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceDTO deviceDTO)
    {

        return toAjax(imsDeviceService.insertImsDevice(deviceDTO));
    }

    /**
     * 修改设备追加属性
     */
    @PreAuthorize("@ss.hasPermi('device:device:edit')")
    @Log(title = "设备追加属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImsDevice imsDevice)
    {
        return toAjax(imsDeviceService.updateImsDevice(imsDevice));
    }

    /**
     * 删除设备追加属性
     */
    @PreAuthorize("@ss.hasPermi('device:device:remove')")
    @Log(title = "设备追加属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imsDeviceService.deleteImsDeviceByIds(ids));
    }
}
